{{left_sidebar_enabled = False
	right_sidebar_enabled=False}}
{{extend 'layout.html'}}

<!-- load dojo and provide config via data attribute -->
<script src="{{=URL('static','dojo/dojo/dojo.js')}}" data-dojo-config="async: true"></script>


<style type="text/css">
	#content_respuesta{
		height : 400px
	}
</style>

<script>require([
	"dojo/ready", 
	"dijit/Editor", 
	"dijit/_editor/plugins/AlwaysShowToolbar", 
	"dijit/_editor/plugins/FontChoice",
    "dijit/_editor/plugins/TextColor",
    "dijit/TitlePane",
    "dijit/MenuBar", 
	"dijit/MenuBarItem",
	"dojo/dom", 
	"dojo/query",
	"dojo/request",
	"dojo/_base/xhr", 
    "dojo/domReady!"
    ], 
function(ready, 
		 Editor, 
		 AlwaysShowToolbar, 
		 FontChoice,
		 TextColor,
		 TitlePane,
		 MenuBar,
		 MenuBarItem,
		 dom, 
		 query,
		 request,
		 xhr){
    	
    function byId(id) { return document.getElementById(id); }	
    	
	var editor = new Editor({
    	height: '',
    	extraPlugins: [
        	AlwaysShowToolbar,
         	'foreColor',
        	'hiliteColor',
          	{name: 'fontName', plainText: false}, 
          	{name: 'fontSize', plainText: false}, 
         	{name: 'formatBlock', plainText: false}
      	]
	}, dom.byId('content_respuesta'));
         
	xhr.get({
		url: '{{=URL(r = request, c = "utils", f="info_consulta")}}',
		handleAs: "text",
			content: {
			numero: dom.byId('id_consulta').value
		},
		load: function(response) {
			var tp;
	        tp = new dijit.TitlePane({title:"Consulta", content: response});
	        dom.byId("titlepane").appendChild(tp.domNode);
       }
	});        
        
    //Menu Bandeja
	var menu = new MenuBar({
	}, "menu_mail");

	menu.addChild( new MenuBarItem({
		id: "p_send",
		label: "Enviar",
		onClick: function(evt){
			byId("result").innerHTML = "<H3 style='color:green'>Enviando...</H3>"
			var send = editor.getValue();
			console.log("mira: " + send);
			if (send == '<br _moz_editor_bogus_node="TRUE" />' | send == '<br />' | send == '') //el editor esta vacio
				byId("result").innerHTML = "<H3 style='color:red'>Esta intentando enviar un mail vacio</H3>";
			else{
	            request.post('{{=URL(r = request, c = "correo", f="correo_send_mail")}}', {
	                data: {
	                	content: send,
	                	idmail: dom.byId('id_mail').value
	                },
	                handleAs: "json"
	            }).then(function(response){
	            	if (response.error == false){
	            		byId("result").innerHTML = "";
	            		window.location = "{{=URL(r = request, c = 'correo', f = 'correo_entrada')}}";
	                }
	                else{
	                	byId("result").innerHTML = "<H3 style='color:red'>La respuesta fue guardada pero ocurrio un error y no se pudo enviar el mail. Por favor, intente enviarlo por otro medio.</H3>";
	                }
	            });
            }
        }
	}));
	
	menu.startup();
        
    
});
</script>

{{=form}}

